/*     */ package com.slim.daoimpl;
/*     */ 
/*     */ import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.appfuse.dao.GenericDao;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateTemplate;
/*     */ 
/*     */ public class GenericDaoHibernate<T, PK extends Serializable>
/*     */   implements GenericDao<T, PK>
/*     */ {
/*  39 */   protected final Log log = LogFactory.getLog(getClass());
/*     */   private Class<T> persistentClass;
/*     */   private HibernateTemplate hibernateTemplate;
/*     */   private SessionFactory sessionFactory;
/*     */ 
/*     */   public GenericDaoHibernate(Class<T> persistentClass)
/*     */   {
/*  51 */     this.persistentClass = persistentClass;
/*     */   }
/*     */ 
/*     */   public GenericDaoHibernate(Class<T> persistentClass, SessionFactory sessionFactory)
/*     */   {
/*  61 */     this.persistentClass = persistentClass;
/*  62 */     this.sessionFactory = sessionFactory;
/*  63 */     this.hibernateTemplate = new HibernateTemplate(sessionFactory);
/*     */   }
/*     */ 
/*     */   public HibernateTemplate getHibernateTemplate() {
/*  67 */     return this.hibernateTemplate;
/*     */   }
/*     */ 
/*     */   public SessionFactory getSessionFactory() {
/*  71 */     return this.sessionFactory;
/*     */   }
/*  77 */   @Autowired
/*     */   @Required
/*     */   public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory;
/*  78 */     this.hibernateTemplate = new HibernateTemplate(sessionFactory);
/*     */   }
/*     */ 
/*     */   public List<T> getAll()
/*     */   {
/*  86 */     return this.hibernateTemplate.loadAll(this.persistentClass);
/*     */   }
/*     */ 
/*     */   public List<T> getAllDistinct()
/*     */   {
/*  94 */     Collection result = new LinkedHashSet(getAll());
/*  95 */     return new ArrayList(result);
/*     */   }
/*     */ 
/*     */   public T get(PK id)
/*     */   {
/* 103 */     Object entity = this.hibernateTemplate.get(this.persistentClass, id);
/*     */ 
/* 105 */     if (entity == null) {
/* 106 */       this.log.warn("Uh oh, '" + this.persistentClass + "' object with id '" + id + "' not found...");
/* 107 */       throw new ObjectRetrievalFailureException(this.persistentClass, id);
/*     */     }
/*     */ 
/* 110 */     return (T) entity;
/*     */   }
/*     */ 
/*     */   public boolean exists(PK id)
/*     */   {
/* 118 */     Object entity = this.hibernateTemplate.get(this.persistentClass, id);
/* 119 */     return entity != null;
/*     */   }
/*     */ 
/*     */   public T save(T object)
/*     */   {
/* 127 */     return this.hibernateTemplate.merge(object);
/*     */   }
/*     */ 
/*     */   public void remove(PK id)
/*     */   {
/* 134 */     this.hibernateTemplate.delete(get(id));
/*     */   }
/*     */ 
/*     */   public List<T> findByNamedQuery(String queryName, Map<String, Object> queryParams)
/*     */   {
/* 142 */     String[] params = new String[queryParams.size()];
/* 143 */     Object[] values = new Object[queryParams.size()];
/*     */ 
/* 145 */     int index = 0;
/* 146 */     for (String s : queryParams.keySet()) {
/* 147 */       params[index] = s;
/* 148 */       values[(index++)] = queryParams.get(s);
/*     */     }
/*     */ 
/* 151 */     return this.hibernateTemplate.findByNamedQueryAndNamedParam(queryName, params, values);
/*     */   }
/*     */ }

/* Location:           C:\.m2\repository\org\appfuse\appfuse-hibernate\2.1.0\
 * Qualified Name:     org.appfuse.dao.hibernate.GenericDaoHibernate
 * JD-Core Version:    0.6.0
 */